<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:variable name="ALPHA_LOWER">
    abcdefghijklmnopqrstuvwxyz
  </xsl:variable>
  <xsl:variable name="ALPHA_UPPER">
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
  </xsl:variable>

  <xsl:template name="convertCase">
    <xsl:param name="pDirection" />
    <xsl:param name="pSource" />

    <xsl:choose>
      <xsl:when test='$pDirection="lower"'>
        <xsl:value-of
          select="translate( $pSource, $ALPHA_UPPER, $ALPHA_LOWER )" />
      </xsl:when>

      <xsl:when test='$pDirection="upper"'>
        <xsl:value-of
          select="translate( $pSource, $ALPHA_LOWER, $ALPHA_UPPER )" />
      </xsl:when>

      <xsl:when test="$pDirection='proper'">
        <xsl:call-template name="convertProperCase">
          <xsl:with-param name="pSource">
            <xsl:value-of
              select="translate( $pSource, $ALPHA_UPPER, $ALPHA_LOWER )" />
          </xsl:with-param>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="$pSource" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="convertProperCase">
    <!-- Recursive Function to convert a string to Proper Case. -->
    <xsl:param name="pSource" />

    <xsl:if test="string-length( $pSource ) &gt; 0">
      <xsl:variable name="vFirstCharacter" select="substring( $pSource, 1, 1 )" />
      <xsl:variable name="vRemainingString" select="substring( $pSource, 2 )" />

      <xsl:call-template name="convertCase">
        <xsl:with-param name="pSource" select="$vFirstCharacter" />
        <xsl:with-param name="pDirection">
          upper
        </xsl:with-param>
      </xsl:call-template>

      <xsl:choose>
        <xsl:when test="contains( $vRemainingString,' ' )">
          <xsl:value-of select="substring-before( $vRemainingString,' ' )" />

          <xsl:call-template name="convertProperCase">
            <xsl:with-param name="pSource"
              select="substring-after( $vRemainingString, ' ' )" />
          </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
          <xsl:value-of select="$vRemainingString" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getSqlTableType">
    <xsl:param name="pFieldType" />
    <xsl:choose>
      <xsl:when test="$pFieldType = 'Identity'">
        <xsl:text>INT AUTO_INCREMENT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Varchar'">
        <xsl:text>VARCHAR</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Char'">
        <xsl:text>CHAR</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Uniqueidentifier'">
        <xsl:text>UNIQUEIDENTIFIER</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Bit'">
        <xsl:text>BIT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Int'">
        <xsl:text>INT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Bigint'">
        <xsl:text>BIGINT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Tinyint'">
        <xsl:text>TINYINT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Smallint'">
        <xsl:text>SMALLINT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Text'">
        <xsl:text>TEXT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'DateTime'">
        <xsl:text>DATETIME</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Identity'">
        <xsl:text>INT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Decimal'">
        <xsl:text>DECIMAL</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Money'">
        <xsl:text>MONEY</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Float'">
        <xsl:text>FLOAT</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Decimal'">
        <xsl:text>DECIMAL</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Money'">
        <xsl:text>MONEY</xsl:text>
      </xsl:when>
      <xsl:when test="$pFieldType = 'Float'">
        <xsl:text>FLOAT</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$pFieldType" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="comment-description">
     <xsl:param name="word"/>
  <!-- </xsl:text> on next line on purpose to get newline -->
     <xsl:variable name="cr"><xsl:text>
</xsl:text></xsl:variable>
     <xsl:choose>
     <xsl:when test="contains($word,$cr)">
         <xsl:value-of select="substring-before($word,$cr)"/>
         <xsl:text>
# </xsl:text>
         <xsl:call-template name="comment-description">
           <xsl:with-param name="word" 
               select="substring-after($word,$cr)"/>
         </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$word"/>
     </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>